<?php

namespace App\Http\Requests;
use App\Models\Order;
use App\Models\Product;
use App\Models\ProductSku;

class SeckillOrderRequest extends Request
{
    public function rules()
    {
        return [
            'address'    => ['required','json'],
            'form_id'           => ['required'],
            'sku_id'     => [
                'required',
                function ($attribute, $value, $fail) {
                    if (!$sku = ProductSku::find($value)) {
                        return $fail('该商品不存在');
                    }
                    $seckill = $sku->product->seckill()->begin()->first();

                    if (!$seckill) {
                        return $fail('该商品不支持秒杀');
                    }
                    if ($seckill->is_before_start) {
                        return $fail('秒杀尚未开始');
                    }
                    if ($seckill->is_after_end) {
                        return $fail('秒杀已经结束');
                    }
                    if (!$sku->product->on_sale) {
                        return $fail('该商品未上架');
                    }
                    if ($sku->stock < 1) {
                        return $fail('该商品已售完');
                    }

                    if ($order = Order::query()
                        // 筛选出当前用户的订单
                        ->where('user_id', $this->user()->id)
                        ->whereHas('items', function ($query) use ($value) {
                            // 筛选出包含当前 SKU 的订单
                            $query->where('product_sku_id', $value);
                        })
                        ->where(function ($query) {
                            // 已支付的订单
                            $query->whereNotNull('paid_at')
                                // 或者未关闭的订单
                                ->orWhere('closed', false);
                        })
                        ->first()) {
                        if ($order->paid_at) {
                            return $fail('你已经抢购了该商品');
                        }
                        return $fail('你已经下单了该商品,请继续支付');
                    }
                }
            ],
        ];
    }
}
